from django.db import models
from utils.common_model import BaseModel
from django.contrib.auth.hashers import check_password, make_password


class User(BaseModel):
    '''
    用户信息表
    '''
    username = models.CharField(max_length=64, verbose_name='用户名', unique=True)
    email = models.EmailField(verbose_name='邮箱', help_text='用于多方式登录', null=True, blank=True)
    phone = models.CharField(max_length=11, verbose_name='手机', help_text='用于短信登录或注册', null=True, blank=True)
    password = models.CharField(max_length=256, verbose_name='密码',
                                help_text='使用django自带的make_password生成的密码，校验使用check_password')
    nick_name = models.CharField(max_length=32, verbose_name='用户昵称', blank=True, null=True)
    gender_choice = (
        (0, '女'),
        (1, '男'),
        (2, '保密')
    )
    gender = models.IntegerField(choices=gender_choice, verbose_name='性别', blank=True, null=True, default=2)
    avatar = models.ImageField(upload_to='avatar/%Y/%m', default='avatar/default.png', verbose_name='头像')
    is_login = models.BooleanField(default=False, verbose_name='是否登录')
    is_superuser = models.BooleanField(default=False, verbose_name='是否是超级管理员')
    # 外键关系
    school = models.ForeignKey(to='school.School', on_delete=models.CASCADE, null=True, blank=True)
    position = models.ForeignKey(to='school.Position', on_delete=models.SET_NULL, null=True, blank=True)
    role = models.ManyToManyField(to='school.Role', db_table='ccsp_user_role', related_name='roles', blank=True)

    class Meta:
        db_table = 'ccsp_user'
        verbose_name_plural = '用户表'

    def __str__(self):
        return self.username

    @staticmethod
    def make_password(raw_password, salt=None, hasher='default'):
        '''
        对密码进行加密
        '''
        return make_password(password=raw_password, salt=salt, hasher=hasher)

    def check_password(self, raw_password):
        '''
        校验密码
        '''
        return check_password(password=raw_password, encoded=self.password)


class Memorandum(BaseModel):
    title = models.CharField(max_length=64, verbose_name='通知标题')
    content = models.TextField()
    user = models.ForeignKey(to=User, on_delete=models.DO_NOTHING, verbose_name='通知的用户')

    class Meta:
        db_table = 'ccsp_memr'

    def __str__(self):
        return self.title


class UserDetail(models.Model):
    user = models.OneToOneField(to=User, on_delete=models.SET_NULL, null=True, blank=True)
    true_name = models.CharField(max_length=64, verbose_name='真实姓名', null=True, blank=True)
    student_num = models.CharField(max_length=15, null=True, blank=True)
    student_img = models.ImageField(upload_to='student/', null=True, blank=True, verbose_name='学生证照片（正面）')
    ID_num = models.CharField(max_length=18, verbose_name='身份证号码', null=True, blank=True)
    ID_img = models.ImageField(upload_to='identity/', null=True, blank=True, verbose_name='身份证照片（正面）')

    class Meta:
        db_table = 'ccsp_user_detail'

    def __str__(self):
        return self.true_name


class Address(models.Model):
    addr = models.CharField(max_length=256)
    is_default = models.BooleanField(default=False)
    addr_point = models.ForeignKey(to='school.AddrPoint', on_delete=models.DO_NOTHING)
    user = models.ManyToManyField(to='User')

    class Meta:
        verbose_name_plural = '地址表'
        db_table = 'ccsp_addr'

    def __str__(self):
        return 'addr_' + self.addr


class Application(BaseModel):
    category_choice = (
        (0, '接单员申请'),
    )
    category = models.IntegerField(choices=category_choice, null=True, verbose_name='申请分类')
    applicant = models.ForeignKey(to='User', on_delete=models.DO_NOTHING, related_name='Applicant',
                                  verbose_name='申请人')
    reviewer = models.ForeignKey(to='User', on_delete=models.DO_NOTHING, related_name='Reviewer', null=True,
                                 verbose_name='审核人')
    state_choice = (
        (0, '审核中'),
        (1, '审核成功'),
        (2, '审核失败')
    )
    state = models.IntegerField(default=0, choices=state_choice, verbose_name='申请状态')
    content = models.TextField(verbose_name='请求正文', null=True)
    school = models.ForeignKey(to='school.School', on_delete=models.DO_NOTHING)

    class Meta:
        db_table = 'ccsp_application'
        verbose_name_plural = '申请表'

    def get_category(self):
        return self.get_category_display()

    def get_state(self):
        return self.get_state_display()
